<template>
  <div>
    <div style="padding-bottom: 10px">
      <el-table
        v-loading="listLoading"
        :data="WorkTasks"
        @selection-change="setSelectRows"
      >
        <el-table-column
          align="center"
          label="任务ID"
          prop="WorkTaskID"
          show-overflow-tooltip
          width="80"
        />
        <el-table-column align="center" label="分单目标" width="130">
          <template #default="{ row }">{{ row.TaskTargetName }}</template>
        </el-table-column>
        <el-table-column align="center" label="指派接单人" width="100">
          <template #default="{ row }">{{ row.TaskStaffName }}</template>
        </el-table-column>
        <el-table-column align="center" label="派单时间" width="200">
          <template #default="{ row }">{{ row.AddTime }}</template>
        </el-table-column>
        <el-table-column align="center" label="任务" width="120">
          <template #default="{ row }">{{ row.ProductName }}</template>
        </el-table-column>
        <el-table-column align="center" label="指定店铺/账号">
          <template #default="{ row }">{{ row.BindStoreAccount }}</template>
        </el-table-column>
        <el-table-column align="center" label="接单状态" width="100">
          <template #default="{ row }">
            <el-text v-if="row.TaskState == 0" type="danger">待接单</el-text>
            <el-text v-if="row.TaskState == 1" type="primary">已接单</el-text>
            <el-text v-if="row.TaskState == 2" type="warning">待审核</el-text>
            <el-text v-if="row.TaskState == 3" type="success">已审核</el-text>
            <el-text v-if="row.TaskState == -2" type="warning">已更换</el-text>
          </template>
        </el-table-column>
        <el-table-column align="center" label="业绩审核状态" width="120">
          <template #default="{ row }">
            <el-text v-if="row.SubmitCheckState == -2" type="danger" />
            <el-text v-if="row.SubmitCheckState == -1" type="danger">
              未通过
            </el-text>
            <el-text v-if="row.SubmitCheckState == 0" type="warning">
              待审核
            </el-text>
            <el-text v-if="row.SubmitCheckState == 1" type="success">
              已通过
            </el-text>
          </template>
        </el-table-column>

        <!-- <el-table-column align="center" label="操作" width="200">
          <template #default="{ row }">
            <div v-if="userInfo.StaffID != row.TaskStaffID">
              <el-button
                v-if="row.isList"
                type="success"
                size="small"
                @click="doAssignTaskList(row)"
              >
                任务清单
              </el-button>
              <el-button
                v-else
                type="success"
                size="small"
                :disabled="row.TaskState != 0"
                @click="doAssignTaskList(row)"
              >
                修改
              </el-button>

              <el-button
                v-if="row.TaskState == 0"
                type="danger"
                size="small"
                @click="cancelTaskList(row)"
              >
                取消分单
              </el-button>
            </div>
          </template>
        </el-table-column> -->
      </el-table>
    </div>
    <assign-order ref="assignRef" @fetch-data="TriggerUp" />
    <!-- <assign-operate ref="assignOperateRef" />
    <assign-designer ref="assignDesignerRef" />
    <assign-videoer ref="assignVideoerRef" />
    <assign-zonghe ref="assignZongheRef" /> -->
  </div>
</template>

<script>
  import { getAssignOrderBtns, cancelWorkTask } from '@/api/worktask'
  import AssignOrder from '@/views/business/components/AssignOrder'

  // import AssignOperate from '@/views/business/components/AssignOperate'
  // import AssignDesigner from '@/views/business/components/AssignDesigner'
  // import AssignVideoer from '@/views/business/components/AssignVideoer'
  // import AssignZonghe from '@/views/business/components/AssignZonghe'

  export default defineComponent({
    name: 'OperateAssign',
    components: {
      AssignOrder,
      // AssignOperate,
      // AssignDesigner,
      // AssignVideoer,
      // AssignZonghe,
    },
    props: {
      actstffpostid: {
        type: Number,
        default: 0,
      },
    },
    emits: ['fetch-data'],
    setup(props, { emit }) {
      // const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')
      const { fetchData } = inject('rootMethods')

      const state = reactive({
        TaskSetState: 0,
        GiftList: [],
        userInfo: {},
        OrderInfo: {},
        OrderID: 0,
        listLoading: false,
        assignRef: null,
        assignDesignerRef: null,
        assignVideoerRef: null,
        assignZongheRef: null,
        ProductList: [],
        StoreList: [],
        WorkTasks: [],
        AssignOrderBtns: [],
        WorktaskInfo: {},
      })

      const showList = (OrderInfo, WorkTasks, WorktaskInfo, GiftList) => {
        state.TaskSetState = WorktaskInfo.OrderList.TaskSetState
        state.OrderInfo = OrderInfo
        state.OrderID = OrderInfo.OrderID
        state.GiftList = GiftList
        getAssignOrderBtn()
        state.WorkTasks = WorkTasks
        state.WorktaskInfo = WorktaskInfo
        state.ProductList = []
        state.ProductList.push(WorktaskInfo.OrderList)
      }

      const cancelTaskList = async (row) => {
        const { msg } = await cancelWorkTask({
          WorkTaskID: row.WorkTaskID,
          state: 0,
        })
        $baseMessage(msg, 'success', 'vab-hey-message-success')
        fetchData()
      }
      // 触发上级
      const TriggerUp = () => {
        emit('fetch-data')
      }
      const doAssignOrder = (item) => {
        console.log('state.ProductList', state.ProductList)

        state.assignRef.showEdit(
          state.OrderID,
          item,
          state.ProductList,
          {},
          state.GiftList
        )
      }
      const doAssignTaskList = (row) => {
        console.log(row, state.AssignOrderBtns, '222888')
        const item = state.AssignOrderBtns.find(
          (itm) => itm.AssignOrderSetID === row.AssignOrderSetID
        )

        console.log(item, 'item123')
        if (state.TaskSetState == 2) {
          state.assignRef.showEdit(
            state.OrderID,
            item,
            state.ProductList,
            row,
            state.GiftList
          )
        } else {
          ElMessage({
            message: '请先完成约定任务设置',
            type: 'warning',
          })
        }
      }
      const getAssignOrderBtn = async () => {
        state.listLoading = true
        // const loginUser = JSON.parse(localStorage.getItem('userInfo'))
        // console.log('loginUser.StaffPostID', JSON.parse(loginUser.StaffPostID))
        // const StaffPostIDs = JSON.parse(loginUser.StaffPostID)

        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        const actstaffpostid = actstaffpost[0][1]

        let FlowState = 7
        if (actstaffpostid == 20) {
          FlowState = 7
        } else if (actstaffpostid == 21) {
          FlowState = 8
        }
        const flow = {
          OrderID: state.OrderID,
          OrderFlowState: FlowState,
          StaffPostID: actstaffpostid,
        }
        const {
          data: { AssignOrderBtns },
        } = await getAssignOrderBtns(flow)
        state.AssignOrderBtns = AssignOrderBtns
        state.listLoading = false
      }
      onMounted(() => {
        state.userInfo = JSON.parse(localStorage.getItem('userInfo'))
        console.log(state.userInfo, ' state.userInfo')
      })

      return {
        ...toRefs(state),
        showList,
        doAssignOrder,
        doAssignTaskList,
        TriggerUp,
        cancelTaskList,
      }
    },
  })
</script>
